const {
  baseUrl,
  getRequest,
  postRequest
} = require("../../../utils/request.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //用户详情
    userDesc: '',
    //弹出框
    userImgShow: false,
    nicknameShow: false,
    userSexShow: false,
    phoneShow: false,
    //性别选择
    userSexList: ['男', '女'],
    userSexChoice: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.http_user_findById()
    this.setData({
      //nickname: options.nickname,
      //userImg: options.userImg
    })
  },
  /**
   * 跳转到个人中心页面
   */
  toUserPage() {
    wx.switchTab({
      url: '/pages/user/index/index',
    })
  },
  /**
   * 用户性别弹出框
   */
  //打开
  onUserSexDialogOpen() {
    this.setData({
      userSexShow: true
    })
  },
  //关闭
  onUserSexDialogClose() {
    this.setData({
      userSexShow: false
    })
  },
  //选择改变事件
  onUserSexDialogChange(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    //console.log(event.detail.value)
    this.setData({
      userSexChoice: event.detail.value
    })
  },
  //选择确定事件
  onUserSexDialogOk() {
    this.setData({
      //userSex: this.data.userSexChoice,
      userSexShow: false
    })
    wx.getStorage({
      key: "userId",
      success: res => {
        //console.log(res.data)
        postRequest(baseUrl + "/front/user/updateUserSex", {
          userId: res.data,
          userSex: this.data.userSexChoice
        }).then(res => {
          if (res.code == 200) {
            this.http_user_findById()
            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 2000
            })
          }
        })
      }
    })
  },
  /**
   * 用户头像弹出框
   */
  onUserImgDialogOpen() {
    this.setData({
      userImgShow: true
    })
  },
  onUserImgDialogClose() {
    this.setData({
      userImgShow: false
    })
  },
  //1. 跳转到相机页面
  onCameraPage() {
    wx.navigateTo({
      url: '/pages/user/desc/camera/camera',
    })
  },
  //2.获取本地相册图片
  onGetLocalImg() {
    // 调用 chooseImage 方法选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success: (res) => {
        const tempFilePath = res.tempFilePaths[0];
        this.setData({
          userImg: tempFilePath,
          userImgShow: false
        });
      },
      fail: (err) => {
        console.error('选择图片失败：', err);
      },
    })
  },
  /**
   * 退出登录
   */
  logout() {
    wx.showModal({
      title: '确定要退出登录吗',
      placeholderText: "请输入信息",
      success(res) {
        if (res.confirm) {
          // res.content获取用户输入信息
          //console.log('用户点击确定', res.content)
          wx.clearStorage() //清理本地数据缓存
          wx.navigateTo({
            url: '/pages/home/login/login',
          })
        } else if (res.cancel) {
          //console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 查询用户详情
   */
  http_user_findById() {
    wx.getStorage({
      key: "userId",
      success: res => {
        //console.log(res.data)
        getRequest(baseUrl + "/front/user/findById", {
          userId: res.data
        }).then(res => {
          //console.log(res.data)
          if (res.code == 200) {
            this.setData({
              userDesc: res.data
            })
          }
        })
      }
    })
  }
})